import cv2
import time

class VideoCamera(object):
    def __init__(self):
        self.cap = cv2.VideoCapture(0)

    def __del__(self):
        self.cap.release()

    def get_frame(self):
        start_time = time.time() # start time
        success, image = self.cap.read() # capture the image frame
        #------process start-------------
        # example
        # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        # image_out = cv2.Canny(gray, 50, 150)
        # example end
        #------process end---------------
        fps = str(1.0 / (time.time() - start_time)) # calculate the fps
        cv2.putText(image, "FPS: {0}".format(fps), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
        # put the fps txt on the image frame, params: image, str, position (Lower left coordinate), font, size, color, thickness
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()
